<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Product');
Globals::requireTable('Order');
Globals::requireTable('Account');
Globals::requireTable('Needslog');

class AccountController extends Controller
{
	protected $UserTable;
	protected $ProductTable;
	protected $OrderTable;
	protected $AccountTable;
	protected $NeedslogTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> false,
		'layoutEnabled'	=> false,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    $config['viewEnabled'] = false;
	    $config['layoutEnabled'] = false;
		parent::__construct($config);
		$this->UserTable      = new UserTable($config);
		$this->ProductTable   = new ProductTable($config);
		$this->OrderTable     = new OrderTable($config);
		$this->AccountTable   = new AccountTable($config);
		$this->NeedslogTable  = new NeedslogTable($config);
	}
	
	public function indexAction()
	{
		
	}
	
	/**
	 * 转租列表
	 * uid/token&machine_type
	 */
	public function rentlistAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	    
	    $list = array();
	    $products = $this->ProductTable->listAll(array('status'=>1,'needs>?'=>3),'rank ASC');
	    foreach ($products as $v){
	        $tmp['img'] = DOMAIN.$v['img'];
	        $tmp['name'] = $v['name'];
	        $tmp['needs'] = $v['needs'];
	        $tmp['pid'] = $v['id'];
	        $list[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$list));
	    exit();
	}
	
	/**
	 * 转租提交
	 * uid/token&machine_type
	 * pid
	 * username
	 * password
	 * days
	 */
	public function dorentAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1));
	        exit();
	    }
	    
	    $data['pid'] = $this->getIntParam('pid');
	    $data['username'] = $this->getParam('username');
	    $data['password'] = $this->getParam('password');
	    $enddays = $this->getIntParam('days');
	    if(empty($data['pid']) || empty($data['username']) || empty($data['password']) || (int)$enddays < 1){
	        echo json_encode(array('returncode'=>-2));
	        exit();
	    }
	    
	    $date = date('Y-m-d');
	    $data['endtime'] = strtotime($date) + 86400 * (int)$enddays;
	    $data['uid'] = $uid;
	    $data['createdate'] = date('Y-m-d H:i:s');
	    $data['status'] = 1;
	    $data['nums_show'] = 3;
	    $this->AccountTable->add($data);
	    echo json_encode(array('returncode'=>0));
	    exit();
	}
	
	/**
	 * 寻租列表
	 * uid/token&machine_type
	 */
	public function needslistAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	     
	    $list = array();
	    $products = $this->ProductTable->listAll(array('status'=>1),'rank ASC');
	    foreach ($products as $v){
	        $tmp['img'] = DOMAIN.$v['img'];
	        $tmp['name'] = $v['name'];
	        $tmp['needs'] = $v['needs'];
	        $tmp['pid'] = $v['id'];
	        $list[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$list));
	    exit();
	}
	
	/**
	 * 寻租提交
	 * uid/token&machine_type
	 * pid
	 */
	public function doneedsAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1));
	        exit();
	    }
	    
	    $data['pid'] = $this->getIntParam('pid');
	    if(empty($data['pid'])){
	        echo json_encode(array('returncode'=>-2));
	        exit();
	    }
	    
	    $data['date'] = strtotime(date('Y-m-d'));
	    $data['uid'] = $uid;
	    $isdo = $this->NeedslogTable->getRow($data);
	    if($isdo['id'] > 0){
	        echo json_encode(array('returncode'=>-3));
	        exit();
	    }
	    
	    $this->NeedslogTable->add($data);
	    echo json_encode(array('returncode'=>0));
	    exit();
	}
	
	/**
	 * 购买平台列表
	 * uid/token&machine_type
	 */
	public function listAction()
	{
        $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	     
	    $result = array();
	    $time = time();
	    $products = $this->ProductTable->listAll(array('status'=>1),'rank ASC');
	    foreach ($products as $v){
	        $tmp['pid'] = $v['id'];
	        $tmp['nameline1'] = $v['name'];
	        $tmp['nameline2'] = $v['name_1'];
	        $tmp['nameline3'] = $v['name_2'];
	        $tmp['imageicon'] = DOMAIN.$v['img'];
	        $tmp['imageright'] = DOMAIN.$v['img_1'];
	        $tmp['imagebg'] = DOMAIN.$v['img_2'];
	        $result[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$result));
	    exit();
	}
	
	/**
	 * 购买账号列表
	 * uid/token&machine_type
	 * pid
	 */
	public function daylistAction()
	{
        $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	    
	    $pid = $this->getIntParam('pid');
	    $product = $this->ProductTable->getRow($pid);
	    if(empty($pid) || $product['status'] != 1){
	        echo json_encode(array('returncode'=>-2,'list'=>array()));
	        exit();
	    }
	    
	    $time = time();
	    $time_day = strtotime(date('Y-m-d'));
	    $result = $day_arr = array();
	    $accounts = $this->AccountTable->listAll(array('status'=>2,'pid'=>$pid,'nums_value>?'=>0,'endtime>?'=>$time));
	    foreach ($accounts as $v){
	        $day_value = ($v['endtime'] - $time_day) / 86400;
	        $day_value = $day_value == (int)$day_value ? $day_value : (int)$day_value + 1;
	        if(!in_array($day_value, $day_arr)){
	            if($v['nums_stock'] < $v['nums_value']){
        	        $tmp['aid'] = $v['id'];
        	        $tmp['pid'] = $pid;
        	        $tmp['imageicon'] = DOMAIN.$product['img'];
        	        $tmp['imageright'] = DOMAIN.$product['img_1'];
        	        $tmp['imagebg'] = DOMAIN.$product['img_2'];
        	        $tmp['nameline1'] = $product['name'];
        	        $tmp['nameline2'] = $product['name_1'];
        	        $tmp['nameline3'] = $product['name_2'];
        	        $tmp['money'] = $v['money'];
        	        $tmp['days'] = $day_value;
        	        $result[] = $tmp;
        	        $day_arr[] = $day_value;
	            }
	        }
	    }
	    
	    echo json_encode(array('returncode'=>0,'list'=>$result));
	    exit();
	}
	
	/**
	 * 订单详情
	 * uid/token&machine_type
	 * orderid
	 */
	public function detailAction(){
        $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'name'=>'','username'=>'','password'=>'','endtime'=>''));
	        exit();
	    }
	    
	    $orderid = $this->getParam('orderid');
	    if(empty($orderid)){
	        echo json_encode(array('returncode'=>-2,'name'=>'','username'=>'','password'=>'','endtime'=>''));
            exit();
	    }
	     
	    $order = $this->OrderTable->getRow(array('orderid'=>$orderid));
	    $product = $this->ProductTable->getRow($order['pid']);
	    $account = $this->AccountTable->getRow($order['aid']);
	    echo json_encode(array('returncode'=>0,'name'=>$product['name'].$order['days'].'天试用期','username'=>$account['username'],'password'=>$account['password'],'endtime'=> date('Y-m-d',$order['endtime'])));
        exit();
	}
	
	protected function out()
	{
		parent::out();
	}
}

Config::extend('AccountController', 'Controller');
